package com.qiyi.player.core.video.engine.dm.agents
{
   import com.qiyi.player.core.IDestroy;
   import com.qiyi.player.core.player.coreplayer.ICorePlayer;
   import com.qiyi.player.core.video.file.IFile;
   import com.qiyi.player.core.video.decoder.IDecoder;
   import flash.utils.Timer;
   import com.qiyi.player.base.logging.ILogger;
   import flash.events.TimerEvent;
   import com.qiyi.player.core.video.file.clientp2pfile.IClientP2PFile;
   import com.qiyi.player.core.video.file.flashp2pfile.IFlashP2PFile;
   import com.qiyi.player.core.video.file.corefile.ICoreFile;
   import flash.system.System;
   import flash.system.Capabilities;
   import com.qiyi.player.core.video.file.actors.datatile.IFlvBlock;
   import com.qiyi.player.core.Config;
   import com.qiyi.player.base.logging.Log;
   import com.qiyi.player.base.utils.Utility;
   
   public class MemoryAgent extends Object implements IDestroy
   {
      
      public function MemoryAgent(param1:ICorePlayer) {
         this._log = Log.getLogger("com.qiyi.player.core.video.engine.dm.agents.MemoryAgent");
         super();
         this._holder = param1;
         this._FP_10_2 = Utility.runtimeSupportsStageVideo();
      }
      
      private var _holder:ICorePlayer;
      
      private var _file:IFile;
      
      private var _decoder:IDecoder;
      
      private var _timer:Timer;
      
      private var _FP_10_2:Boolean = false;
      
      private var _log:ILogger;
      
      public function bind(param1:IFile, param2:IDecoder) : void {
         this._file = param1;
         this._decoder = param2;
      }
      
      public function start() : void {
         if(this._timer == null)
         {
            this._timer = new Timer(10000);
            this._timer.addEventListener(TimerEvent.TIMER,this.onTimer);
         }
         if(!this._timer.running)
         {
            this._timer.reset();
            this._timer.start();
         }
      }
      
      public function stop() : void {
         if(this._timer)
         {
            this._timer.stop();
         }
      }
      
      public function destroy() : void {
         if(this._timer)
         {
            this._timer.removeEventListener(TimerEvent.TIMER,this.onTimer);
            this._timer.stop();
            this._timer = null;
         }
         this._file = null;
         this._holder = null;
         this._decoder = null;
      }
      
      private function onTimer(param1:TimerEvent) : void {
         if(this._file == null)
         {
            return;
         }
         if(this._file is IClientP2PFile)
         {
            this.releaseClientP2PFileData();
         }
         else if(this._file is IFlashP2PFile)
         {
            this.releaseFlashP2PFileData();
         }
         else if(this._file is ICoreFile)
         {
            this.releaseCoreFileData();
         }
         
         
      }
      
      private function releaseClientP2PFileData() : void {
      }
      
      private function releaseFlashP2PFileData() : void {
      }
      
      private function releaseCoreFileData() : void {
         if(this._file == null)
         {
            return;
         }
         var _loc1_:ICoreFile = this._file as ICoreFile;
         if(_loc1_ == null)
         {
            return;
         }
         if(_loc1_.blockCount == 0)
         {
            return;
         }
         var _loc2_:Number = this._holder.runtimeData.maxMemorySize << 20;
         var _loc3_:uint = 0;
         if(this._FP_10_2)
         {
            _loc3_ = System["privateMemory"];
         }
         else
         {
            _loc3_ = System.totalMemory;
         }
         if(_loc3_ < _loc2_)
         {
            return;
         }
         if(Capabilities.isDebugger)
         {
            this._log.info("memory agent flash player is Debugger!");
            if(this._FP_10_2)
            {
               _loc3_ = System["privateMemory"];
            }
            else
            {
               _loc3_ = System.totalMemory;
            }
            if(_loc3_ < _loc2_)
            {
               return;
            }
         }
         this._log.info("the memory is too large! TotalMemory: " + (_loc3_ >> 20) + "MB, the system setting max memory is:" + this._holder.runtimeData.maxMemorySize + "MB");
         var _loc4_:int = _loc3_ - _loc2_;
         var _loc5_:* = 0;
         var _loc6_:IFlvBlock = null;
         _loc5_ = 0;
         while(_loc1_.readingIndex >= 0 && !(_loc5_ == _loc1_.readingIndex) && _loc5_ >= 0 && _loc5_ < _loc1_.blockCount)
         {
            _loc6_ = _loc1_.findBlockAt(_loc5_);
            if(!_loc6_.loading && _loc6_.endTime >= _loc6_.segment.endTime)
            {
               _loc4_ = _loc4_ - _loc6_.size;
               _loc1_.removeBlockAt(_loc5_);
               if(_loc4_ <= 0)
               {
                  return;
               }
            }
            else
            {
               _loc5_++;
            }
         }
         _loc5_ = 0;
         while(_loc1_.readingIndex >= 0 && _loc5_ < _loc1_.readingIndex && _loc1_.blockCount > 0)
         {
            _loc6_ = _loc1_.findBlockAt(_loc5_);
            if(!_loc6_.loading)
            {
               _loc4_ = _loc4_ - _loc6_.size;
               _loc1_.removeBlockAt(_loc5_);
               if(_loc4_ <= 0)
               {
                  return;
               }
            }
            else
            {
               _loc5_++;
            }
         }
         _loc5_ = _loc1_.blockCount - 1;
         while(_loc1_.readingIndex >= 0 && _loc5_ > _loc1_.readingIndex && _loc5_ >= 0 && _loc5_ < _loc1_.blockCount)
         {
            _loc6_ = _loc1_.findBlockAt(_loc5_);
            if(_loc6_.startTime - this._decoder.time <= Config.PRELOAD_NEXT_SEGMENT)
            {
               break;
            }
            if(!_loc6_.loading)
            {
               _loc4_ = _loc4_ - _loc6_.size;
               _loc1_.removeBlockAt(_loc5_);
            }
            _loc5_--;
            if(_loc4_ <= 0)
            {
               return;
            }
         }
      }
   }
}
